#Universidad del Valle de Guatemala
#Sistemas Operativos Avanzados
#David Ytzen Hsieh Lo #08225

require 'socket'              

server = TCPServer.open(81)

puts "----------------------------------------------"
puts "Starting Ifrit server..."
puts "----------------------------------------------"
 
File.open('Logo.txt', 'r') do |f1|
	while line = f1.gets
		puts line
	end
end

puts "Server running..."
puts "----------------------------------------------"

loop {                        
	socket = server.accept      
	request = socket.readline
	host = socket.readline
 	validGET = request.match(/GET .* HTTP\/1\.1/)

	unless (validGET)
		socket.puts "HTTP/1.1 400 Bad Request"
		socket.close
		next
	end

	cTime = Time.now
	cTimeA = cTime.to_a
	fCTime = Time.utc(*cTimeA)
	socket.puts "HTTP/1.1 200 OK"
	socket.puts "Server.Apache/1.3.3.7 (Unix) (CentOS/Linux)"
	socket.puts "Date: " +  fCTime.inspect
	socket.puts "Content-Type: text/html"
	socket.puts "Content-Length: 311"
	socket.puts ""


	File.open("index.html", 'r') { |f|
		while (line = f.gets)
			socket.puts line
		end

	
	
	
	
	
	}
#	socket.puts "<html>"
#	socket.puts "<body bgcolor = black text=white> Nice ;) </body>"
#	socket.puts "</html>"


	socket.close              
}
















require 'socket'

server = TCPServer.new('127.0.0.1', '8080')

while socket = server.accept
    request = socket.readline

    validGET = request.match(/GET .* HTTP\/1\.1/)

    unless (validGET)
        socket.puts "HTTP/1.1 400 Bad Request"
        socket.close
        next
    end

    file = request.split(' ')[1]
    file = '.' + file

    unless ( File::exists?(file) )
        socket.puts "HTTP/1.1 404 File Not Found"
	puts "NO"
        socket.close
        next
    end

    socket.puts "HTTP/1.1 200 OK\n"
    socket.puts "Connection: close\n"
    socket.puts "Content-Type: text/html\n"
    File.open(file, 'r') { |f|
        while (line = f.gets)
            socket.puts line
        end
    }

    socket.close
end

